﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.dao;
using ProjectScheduling_web.model;

namespace ProjectScheduling_web.Controllers.U
{
    /// <summary>
    /// 收款信息
    /// </summary>
    public class ReceiveMoneyController : BController
    {
        private readonly static ReceiveMoneyDao @base = ReceiveMoneyDao.Base;
        private readonly static ReceiveMoney_ContractDao dao = ReceiveMoney_ContractDao.Base;

        [HttpGet]
        public IActionResult Search(int page = 1, int row = 15, string key = "", string start = "", string end = "")
        {
            var _start = DateTime.TryParse(start, out DateTime st) ? st : DateTime.MinValue;
            var _end = DateTime.TryParse(end, out DateTime et) ? et : DateTime.MaxValue;
            var data = @base.FindAll(page, row,
                it => (string.IsNullOrEmpty(key) || (it.ProjectName != null && it.ProjectName.Contains(key)) || (it.CustomerName != null && it.CustomerName.Contains(key))) &&
                (string.IsNullOrEmpty(start) || it.ReceiveDate >= _start) &&
                (string.IsNullOrEmpty(end) || it.ReceiveDate <= _end)
                );
            return Success(data);
        }
        [HttpPost]
        public IActionResult Add([FromBody] ReceiveMoney req)
        {
            if (req == null) return Fail("当前请求错误");
            if (HttpContext.Items["Current_Msg"] is not Member mem) return Fail("获取登录状态有误");
            req.CreateTime = DateTime.Now;
            req.CreaterId = mem.Id;
            req.CreaterName = mem.Name;
            var t = DictionaryDataDao.Base.Find(z => z.Category == model.Enums.EnumDictionaryCategory.收款方式 && z.Value == req.ReceiveTypeId);
            if (t != null)
            {
                req.ReceiveTypeName = t.Name;
            }
            var dep = DictionaryDataDao.Base.Find(z => z.Category == model.Enums.EnumDictionaryCategory.部门 && z.Value == req.OrgId);
            if (dep != null)
            {
                req.OrgName = dep.Name;
            }
            var ct = @base.Count(it => it.CreateTime.Date == DateTime.Now.Date);
            req.Code = $"RK_{DateTime.Now:yyyyMMdd}_{ct + 1}";
            var id = @base.Add(req);
            foreach (var item in req.Contracts ?? [])
            {
                item.ReceiveMoneyId = id;
                item.CreateTime = DateTime.Now;
                item.CreaterId = mem.Id;
                item.CreaterName = mem.Name;
                dao.Add(item);
            }
            return Success("发布成功");
        }
        [HttpGet]
        public IActionResult Remove(int id)
        {
            var ct = @base.Delete(id);
            ct += dao.Delete(it => it.ReceiveMoneyId == id);
            return ct > 0 ? Success("删除成功") : Fail("没有待删除的信息");
        }
        [HttpGet]
        public IActionResult SearchContract(int page = 1, int row = 15, string key = "", string noids = "")
        {
            var ids = noids.Split(',').Select(it => int.TryParse(it, out int id) ? id : 0).ToList();
            var data = dao.Check_Contract_Datas(page, row, key, ids);
            return Success(data);
        }
        [HttpGet]
        public IActionResult Find(int id)
        {
            var obj = @base.Find(id);
            if (obj == null) return Fail("没有找到相关信息");
            var contracts = dao.FindAll(it => it.ReceiveMoneyId == id);
            obj.Contracts = contracts;
            return Success(obj);
        }
    }
}
